package com.yy.wga.mp.module.talent.controller;

import com.yy.wga.common.core.dto.Result;
import com.yy.wga.common.util.common.EnvUtils;
import com.yy.wga.common.util.common.StringUtils;
import com.yy.wga.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.common.dto.UserCacheDTO;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.module.talent.request.FeedReq;
import com.yy.wga.mp.module.talent.request.LiveReq;
import com.yy.wga.mp.module.talent.request.TalentReq;
import com.yy.wga.mp.module.talent.response.FeedResp;
import com.yy.wga.mp.module.talent.response.LiveResp;
import com.yy.wga.mp.module.talent.response.TalentResp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 达人接口管理
 */
@RestController
@RequestMapping("api/talent")
public class TalentController {

    @Resource
    private TalentFeign talentFeign;


    /**
     * 获取已绑定达人信息
     **/
    @PostMapping("getBindTalentList")
    public Result<TalentResp.BindTalentList> getBindTalentList(@RequestBody @Valid TalentReq.GetBindTalentList req) {
        com.yy.wga.resource.feign.request.TalentReq.GetBindTalentList getBindTalentList = BeansUtils.map(req, com.yy.wga.resource.feign.request.TalentReq.GetBindTalentList.class);
        Result<com.yy.wga.goods.feign.response.TalentResp.BindTalentList> result = talentFeign.getBindTalentList(getBindTalentList);
        if (result.isSuccess()) {
            return Result.success(BeansUtils.map(result.getData(), TalentResp.BindTalentList.class));
        }
        return Result.fail(result.getCode(), result.getMessage());
    }

    /**
     * 获取短视频列表
     **/
    @PostMapping("getFeedList")
    public Result<FeedResp.FeedList> getFeedList(@RequestBody @Valid FeedReq.GetFeedList req) {
        com.yy.wga.resource.feign.request.FeedReq.GetFeedList getFeedList = BeansUtils.map(req, com.yy.wga.resource.feign.request.FeedReq.GetFeedList.class);
        UserCacheDTO userCache = RequestUserHolder.getUserCache();
        getFeedList.setGrade(userCache.getGrade());
        getFeedList.setLevel(userCache.getLevel());
        Result<com.yy.wga.resource.feign.response.FeedResp.FeedList> result = talentFeign.getFeedList(getFeedList);
        if (result.isSuccess()) {
            return Result.success(BeansUtils.map(result.getData(), FeedResp.FeedList.class));
        }
        return Result.fail(result.getCode(), result.getMessage());
    }

    /**
     * 获取短视频列表
     **/
    @GetMapping("getFeedUrlList")
    public Result<List<String>> getFeedUrlList() {
        List<String> urlList = new ArrayList<>();
        String properties = EnvUtils.get("live.default.urlList");
        if (StringUtils.isNotBlank(properties)) {
            urlList = Arrays.stream(properties.split(",")).collect(Collectors.toList());
        }
        return Result.success(urlList);
    }


    /**
     * 获取短视频详情
     **/
    @PostMapping("getFeedPromotionInfo")
    public Result<FeedResp.FeedPromotionInfo> getFeedPromotionInfo(@RequestBody @Valid FeedReq.GetFeedPromotionInfoReq req) {
        com.yy.wga.resource.feign.request.FeedReq.GetFeedPromotionInfoReq getFeedPromotionInfoReq = BeansUtils.map(req, com.yy.wga.resource.feign.request.FeedReq.GetFeedPromotionInfoReq.class);
        Result<com.yy.wga.resource.feign.response.FeedResp.FeedPromotionInfo> result = talentFeign.getFeedPromotionInfo(getFeedPromotionInfoReq);
        if (result.isSuccess()) {
            return Result.success(BeansUtils.map(result.getData(), FeedResp.FeedPromotionInfo.class));
        }
        return Result.fail(result.getCode(), result.getMessage());
    }


    /**
     * 获取一个直播间
     **/
    @PostMapping("getOneLive")
    public Result<LiveResp.LiveInfo> getOneLive(@RequestBody @Valid LiveReq.GetOneLive req) {
        com.yy.wga.resource.feign.request.LiveReq.GetOneLive getFeedPromotionInfoReq = BeansUtils.map(req, com.yy.wga.resource.feign.request.LiveReq.GetOneLive.class);
        Result<com.yy.wga.goods.feign.response.LiveResp.LiveInfo> result = talentFeign.getOneLive(getFeedPromotionInfoReq);
        if (result.isSuccess()) {
            return Result.success(BeansUtils.map(result.getData(), LiveResp.LiveInfo.class));
        }
        return Result.fail(result.getCode(), result.getMessage());
    }

    /**
     * 分享直播间二维码
     **/
    @PostMapping("getLiveShareQrCode")
    public Result<LiveResp.LiveShareQcCode> getLiveShareQrCode(@RequestBody @Valid LiveReq.GetLiveShareQcCode req) {
        com.yy.wga.resource.feign.request.LiveReq.GetLiveShareQcCode getLiveShareQcCode = BeansUtils.map(req, com.yy.wga.resource.feign.request.LiveReq.GetLiveShareQcCode.class);
        getLiveShareQcCode.setOpenId(RequestUserHolder.getUserCache().getOpenId());
        Result<com.yy.wga.goods.feign.response.LiveResp.LiveShareQcCode> result = talentFeign.getLiveShareQrCode(getLiveShareQcCode);
        if (result.isSuccess()) {
            return Result.success(BeansUtils.map(result.getData(), LiveResp.LiveShareQcCode.class));
        }
        return Result.fail(result.getCode(), result.getMessage());
    }
}
